package com.igeek.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author fengqq
 * @Description 基于内存的认证用户 配置类
 * @Date 2024/9/13  14:31
 *
 * 自定义Security配置类：
 * 方式一：在application.properties文件中 使用指定的前缀spring.security来操作 内存中的认证用户信息
 * 方式二：1.@Configuration+@EnableWebSecurity+@Bean 替换底层组件 SecurityFilterChain
 *
 * 方式二：2.@Configuration+@EnableWebSecurity+extends WebSecurityConfigurerAdapter
 *          重写  protected void configure(AuthenticationManagerBuilder auth)
 */
//@Configuration
//开启Security安全认证
//@EnableWebSecurity
public class SecurityInMemoryConfig extends WebSecurityConfigurerAdapter {
    //加密方式
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //基于内存完成认证用户信息
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")//用户名
                .password(passwordEncoder().encode("admin"))//密码
                .roles("ADMIN");//角色
        auth.inMemoryAuthentication()
                .withUser("user")//用户名
                .password(passwordEncoder().encode("user"))//密码
                .roles("USER");//角色
        auth.inMemoryAuthentication()
                .withUser("joker")//用户名
                .password(passwordEncoder().encode("joker"))//密码
                .roles("JOKER");//角色
    }
}
